package com.mindskip.xzs.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.constant.CommonRedisKey;
import com.mindskip.xzs.domain.User;
import com.mindskip.xzs.domain.enums.RoleEnum;
import com.mindskip.xzs.repository.NewUserMapper;
import com.mindskip.xzs.service.NewUserService;
import com.mindskip.xzs.utility.excel.EasyExcelUtil;
import com.mindskip.xzs.viewmodel.IdNameKV;
import com.mindskip.xzs.viewmodel.admin.exam.FinishPagerRes;
import com.mindskip.xzs.viewmodel.admin.user.EmployeeReq;
import com.mindskip.xzs.viewmodel.admin.user.EmployeeRes;
import com.mindskip.xzs.viewmodel.admin.user.UserCreditVM;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class NewUserServiceImpl extends ServiceImpl<NewUserMapper, User> implements NewUserService {
    @Resource
    private RedisTemplate<String,List<IdNameKV>> redisTemplate;


    @Override
    public PageInfo<EmployeeRes> queryUserList(EmployeeReq employeeReq) {
        PageHelper.startPage(employeeReq.getPageNum(), employeeReq.getPageSize(), "id desc");
        employeeReq.setRole(RoleEnum.STUDENT.getCode());
        List<EmployeeRes> employeeRes = this.baseMapper.selectUserList(employeeReq);
        if (CollectionUtil.isEmpty(employeeRes)) {
            return new PageInfo<>();
        }
        // 积分
        List<Long> userIds = employeeRes.stream().map(EmployeeRes::getId).collect(Collectors.toList());
        Map<Long, Integer> userCreditMap = baseMapper.selectUserCredit(userIds).stream().collect(Collectors.toMap(UserCreditVM::getUserId, UserCreditVM::getCredit));
        employeeRes.forEach(emp -> emp.setCredit(userCreditMap.get(emp.getId()) == null ? "0分" : userCreditMap.get(emp.getId()) + "分"));
        return new PageInfo<>(employeeRes);
    }

    @Override
    public void exportUserList(EmployeeReq employeeReq, HttpServletResponse response) {
        PageInfo<EmployeeRes> employeeResPageInfo = queryUserList(employeeReq);
        List<EmployeeRes> list = employeeResPageInfo.getList();
        EasyExcelUtil.onceExportByEntity("员工信息", response, list, EmployeeRes.class, null, null);
    }

    @Override
    public List<IdNameKV> queryUserKVListInCache(String keyword) {
        List<IdNameKV> userKVResList = redisTemplate.opsForValue().get(CommonRedisKey.ALL_USER_KV_KEY);
        if (StringUtils.isNotBlank(keyword) && userKVResList != null) {
            userKVResList = userKVResList.stream().filter(userKVRes -> userKVRes.getName().contains(keyword)).collect(Collectors.toList());
        }
        return userKVResList;
    }

    @Override
    public List<IdNameKV> queryUserKVListForSearch(Long deptId, String keyword) {
        return baseMapper.queryUserKVListForSearch(deptId, keyword);
    }

    /**
     * 更新缓存
     */
    @Override
    public void updateUserKVInCache() {
        List<IdNameKV> userKVList = baseMapper.queryUserKVList();
        if (CollectionUtil.isNotEmpty(userKVList)) {
            redisTemplate.opsForValue().set(CommonRedisKey.ALL_USER_KV_KEY, userKVList, 30, TimeUnit.DAYS);
        }
    }
}
